package com.hakim.wfb.admin.filter;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.dao.RoleDao;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.validate.ValidateFilter;
import com.hkfs.fundamental.validate.annotaion.ValidateFilterBean;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@ValidateFilterBean
public class ModifyRoleFilter extends ValidateFilter<ModifyRoleFilter.ModifyRoleFilterParam> {

    @Autowired
    private RoleDao roleDao;

    @Override
    protected void prcoess(ModifyRoleFilterParam param, ResultEx resultEx) {
        Map<String, Object> existParam = new HashMap<String, Object>();
        existParam.put("roleId", param.getId());
        existParam.put("roleName", param.getRoleName());
        int count = roleDao.modifyExists(existParam);
        if (count > 0) {
            resultEx.makeResult(ErrorCodeEnum.ROLE_NAME_EXISTS.getCode(), ErrorCodeEnum.ROLE_NAME_EXISTS.getMessage());
            return;
        }
        resultEx.makeSuccessResult();
    }

    public static class ModifyRoleFilterParam {
        @NotNull(message = "角色ID不能为空")
        private int id;
        @NotEmpty(message = "角色名不能为空")
        private String roleName;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
    }
}
